
clear all; 

dim.elem=100; 
dim.rep =500; 

%% Create a [dim.elem cell of names] 
fields=fnumcell('X',1:dim.elem); 

randIndex=rand(dim.elem,1); 

%% Randomly assign in the elemnts are calibrated 'C' or 'N' 
for ii=1:dim.elem
    fldnm=fields{ii};
    if randIndex(ii) <= 0.3
        par.(fldnm).dist='C';
        par.(fldnm).val=10;
    elseif randIndex(ii) <= 0.5 
        par.(fldnm).dist='N';
        par.(fldnm).val=10*randIndex(ii);
        par.(fldnm).mean=round( 10*randIndex(ii) ); 
        par.(fldnm).std=abs(round( 10*randIndex(ii) )/50); 
    elseif randIndex(ii) <= 0.7 
        par.(fldnm).dist='G';
        par.(fldnm).val=abs(randIndex(ii));
        par.(fldnm).mean=abs(round( 10*randIndex(ii) ));
        par.(fldnm).std=abs(round( 10*randIndex(ii) )/5);
    elseif randIndex(ii) <= 0.9
        par.(fldnm).dist='B';
        par.(fldnm).val=abs(randIndex(ii));
        par.(fldnm).mean=abs(randIndex(ii));
        par.(fldnm).std=abs(randIndex(ii))/2;        
    end
end    

clear fields; 
fields=fieldnames(par); 
dim.par=length(fields); 

%% Extract positions estimated and calibrated 
%% Organize the structure differently 
pos.cal=zeros(dim.par,1); 
for ii=1:dim.par 
    fldnm=fields{ii};
    if strcmpi(par.(fldnm).dist,'C')==true
        pos.cal(ii)=1;
    end
    parIn.(fldnm)=par.(fldnm).val;     
    par2.dist.(fldnm)=par.(fldnm).dist; 
    par2.val.(fldnm)=par.(fldnm).val; 
end 

%% Extract the vector 
pos.est=find( pos.cal==0 ); 
pos.cal=find( pos.cal==1 ); 
parval=cell2mat(struct2cell(par2.val)); 

%% Extract the positions estimated and calibrated 
dim.est=length(pos.est); 
dim.cal=length(pos.cal); 
fprintf('Number estimated =%d \n',dim.est); 
names.est=fields(pos.est); 
names.cal=fields(pos.cal); 

%% Assign new vals to the estimated positions 
newvals=randi([100,500],dim.est,1); 

%% How it is written inside the model 
%% Assign newvals to the fields in the model 
tic; 
fprintf('Number repetitions =%d \n',dim.rep); 
for jj=1:dim.rep;
    for ii=1:dim.est
        fldnm=names.est{ii};
        par2.val.(fldnm)=newvals(ii);
    end
end
toc; 

%% For the computation of the prior 
tic; 
parval2=cell2mat(struct2cell(par2.val)); 
toc; 
